package com.sali.b_design_pattern.f_responsibility_pattern.strategy.method02.handler;

import com.sali.b_design_pattern.f_responsibility_pattern.strategy.method02.config.ProductCheckHandlerConfig;
import com.sali.b_design_pattern.f_responsibility_pattern.strategy.method02.pojo.entity.Result;
import com.sali.b_design_pattern.f_responsibility_pattern.strategy.method02.pojo.vo.ProductVO;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 处理器抽象类
 *
 * @author sali
 * @date 2025/3/16 10:04
 */
@Component
public abstract class AbstractCheckHandler {

    // 当前处理器持有下一个处理器的引用
    @Getter
    @Setter
    protected AbstractCheckHandler nextHandler;

    // 处理器配置
    @Setter
    @Getter
    protected ProductCheckHandlerConfig config;

    // 处理器执行方法
    public abstract Result handle(ProductVO param);

    // 链路传递
    protected Result next(ProductVO param) {
        //下一个链路没有处理器了，直接返回
        if (Objects.isNull(nextHandler)) {
            return Result.success();
        }

        //执行下一个处理器
        return nextHandler.handle(param);
    }

}
